IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'Party')
EXEC sys.sp_executesql N'CREATE SCHEMA [Party] AUTHORIZATION [dbo]'

GO
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'Address')
EXEC sys.sp_executesql N'CREATE SCHEMA [Address] AUTHORIZATION [dbo]'

GO
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'Note')
EXEC sys.sp_executesql N'CREATE SCHEMA [Note] AUTHORIZATION [dbo]'

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PreferenceType]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PreferenceType](
	[PreferenceTypeId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PreferenceType] PRIMARY KEY CLUSTERED 
(
	[PreferenceTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[RegistrationAuthority]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[RegistrationAuthority](
	[RegistrationAuthorityId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_RegistrationAuthority] PRIMARY KEY CLUSTERED 
(
	[RegistrationAuthorityId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Note].[NoteStatus]') AND type in (N'U'))
BEGIN
CREATE TABLE [Note].[NoteStatus](
	[NoteStatusId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [nvarchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_NoteStatus_CreatedDateTime]  DEFAULT (getdate()),
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_NoteStatus_UpdatedDateTime]  DEFAULT (getdate()),
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_NoteStatus] PRIMARY KEY NONCLUSTERED 
(
	[NoteStatusId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Note].[NoteStatus]') AND name = N'IX_NoteStatus_Description')
CREATE CLUSTERED INDEX [IX_NoteStatus_Description] ON [Note].[NoteStatus] 
(
	[Description] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[RoleType]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[RoleType](
	[RoleTypeId] [int] IDENTITY(1,1) NOT NULL,
	[Code] [varchar](50) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_RoleType] PRIMARY KEY CLUSTERED 
(
	[RoleTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[TaxationValidationType]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[TaxationValidationType](
	[TaxationValidationTypeId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_TFNValidationType] PRIMARY KEY CLUSTERED 
(
	[TaxationValidationTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[IdentifierType]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[IdentifierType](
	[IdentifierTypeId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_IdentiferType] PRIMARY KEY NONCLUSTERED 
(
	[IdentifierTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Party].[IdentifierType]') AND name = N'IX_IdentiferType_Description')
CREATE UNIQUE CLUSTERED INDEX [IX_IdentiferType_Description] ON [Party].[IdentifierType] 
(
	[Description] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[MaritalStatus]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[MaritalStatus](
	[MaritialStatusId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL CONSTRAINT [DF_MartitalStatus_CreatedBy]  DEFAULT (''),
	[CreatedDateTime] [datetime] NULL,
	[UpdatedBy] [varchar](50) NOT NULL CONSTRAINT [DF_MartitalStatus_UpdatedBy]  DEFAULT (''),
	[UpdatedDateTime] [datetime] NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_MartitalStatus] PRIMARY KEY CLUSTERED 
(
	[MaritialStatusId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[Occupation]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[Occupation](
	[OccupationId] [int] IDENTITY(1,1) NOT NULL,
	[OccupationCode] [char](10) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_Occupation] PRIMARY KEY CLUSTERED 
(
	[OccupationId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyAddressUsage]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyAddressUsage](
	[PartyAddressUsageId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_AddressUsage] PRIMARY KEY CLUSTERED 
(
	[PartyAddressUsageId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyRelationshipType]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyRelationshipType](
	[PartyRelationshipTypeId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyRelationshipType] PRIMARY KEY CLUSTERED 
(
	[PartyRelationshipTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Address].[AddressType]') AND type in (N'U'))
BEGIN
CREATE TABLE [Address].[AddressType](
	[AddressTypeId] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_AddressType_1] PRIMARY KEY CLUSTERED 
(
	[AddressTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Address].[ElectronicAddressType]') AND type in (N'U'))
BEGIN
CREATE TABLE [Address].[ElectronicAddressType](
	[ElectronicAddressTypeId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_AddressType] PRIMARY KEY CLUSTERED 
(
	[ElectronicAddressTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Address].[Country]') AND type in (N'U'))
BEGIN
CREATE TABLE [Address].[Country](
	[CountryId] [int] IDENTITY(1,1) NOT NULL,
	[OfficialName] [varchar](50) NOT NULL,
	[ShortName] [varchar](50) NOT NULL,
	[CountryCodeA3] [char](3) NOT NULL,
	[CountryCodeA2] [char](2) NOT NULL,
	[CountryNumber] [int] NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_Country] PRIMARY KEY NONCLUSTERED 
(
	[CountryId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Address].[Country]') AND name = N'IX_CountryName')
CREATE UNIQUE CLUSTERED INDEX [IX_CountryName] ON [Address].[Country] 
(
	[OfficialName] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO





SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyClassificationType]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyClassificationType](
	[PartyClassificationTypeId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_PartyClassificationType_CreatedDateTime]  DEFAULT (getdate()),
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_PartyClassificationType_UpdatedDateTime]  DEFAULT (getdate()),
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyClassificationType] PRIMARY KEY NONCLUSTERED 
(
	[PartyClassificationTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Party].[PartyClassificationType]') AND name = N'IX_PartyClassificationType_Description')
CREATE CLUSTERED INDEX [IX_PartyClassificationType_Description] ON [Party].[PartyClassificationType] 
(
	[Description] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Party].[PartyClassificationType]') AND name = N'IX_PartyClassificationType')
CREATE UNIQUE NONCLUSTERED INDEX [IX_PartyClassificationType] ON [Party].[PartyClassificationType] 
(
	[Description] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyCountryRelationshipType]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyCountryRelationshipType](
	[PartyCountryRelationshipTypeId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [nvarchar](100) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_RegistrationClassification_CreatedDateTime]  DEFAULT (getdate()),
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_RegistrationClassification_UpdatedDateTime]  DEFAULT (getdate()),
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyCountryRelationshipType] PRIMARY KEY NONCLUSTERED 
(
	[PartyCountryRelationshipTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Party].[PartyCountryRelationshipType]') AND name = N'IX_PartyCountryRelationshipType_Description')
CREATE UNIQUE CLUSTERED INDEX [IX_PartyCountryRelationshipType_Description] ON [Party].[PartyCountryRelationshipType] 
(
	[Description] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[Gender]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[Gender](
	[GenderId] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[ISOGenderCode] [char](1) NULL,
	[CreatedBy] [varchar](50) NOT NULL CONSTRAINT [DF_GenderType_CreatedBy]  DEFAULT (''),
	[CreatedDateTime] [datetime] NULL,
	[UpdatedBy] [varchar](50) NOT NULL CONSTRAINT [DF_GenderType_UpdatedBy]  DEFAULT (''),
	[UpdatedDateTime] [datetime] NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_GenderType] PRIMARY KEY CLUSTERED 
(
	[GenderId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyType]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyType](
	[PartyTypeId] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](50) NOT NULL,
	[Code] [varchar](1) NOT NULL,	
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyType] PRIMARY KEY CLUSTERED 
(
	[PartyTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyNote]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyNote](
	[PartyNoteId] [int] IDENTITY(1,1) NOT NULL,
	[PartyId] [int] NOT NULL,
	[NoteId] [int] NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_PartyNote_CreatedDateTime]  DEFAULT (getdate()),
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_PartyNote_UpdatedDateTime]  DEFAULT (getdate()),
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyNote] PRIMARY KEY CLUSTERED 
(
	[PartyNoteId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyIdentifier]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyIdentifier](
	[PartyIdentifierId] [int] IDENTITY(1,1) NOT NULL,
	[PartyId] [int] NOT NULL,
	[IdentifierTypeId] [int] NOT NULL,
	[Identifier] [varchar](50) NOT NULL,
	[ValidFromDateTime] [datetime] NOT NULL,
	[ValidToDateTime] [datetime] NULL,
	[RegistrationAuthorityId] [int] NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyIdentifier] PRIMARY KEY CLUSTERED 
(
	[PartyIdentifierId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Party].[PartyIdentifier]') AND name = N'IX_PartyIdentifierIdentifierType')
CREATE NONCLUSTERED INDEX [IX_PartyIdentifierIdentifierType] ON [Party].[PartyIdentifier] 
(
	[Identifier] ASC,
	[IdentifierTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[IndividualParty]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[IndividualParty](
	[PartyId] [int] NOT NULL,
	[GenderId] [int] NOT NULL,
	[GivenName] [varchar](50) NOT NULL,
	[MiddleName] [varchar](50) NOT NULL,
	[FamilyName] [varchar](50) NOT NULL,
	[DateOfBirth] [datetime] NULL,
	[DateOfDeath] [datetime] NULL,
	[OccupationId] [int] NULL,
	[MaritalStatusId] [int] NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_IndividualParty] PRIMARY KEY CLUSTERED 
(
	[PartyId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyAddress]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyAddress](
	[PartyAddressId] [int] IDENTITY(1,1) NOT NULL,
	[PartyId] [int] NOT NULL,
	[AddressId] [int] NOT NULL,
	[PartyAddressUsageId] [int] NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyAddress] PRIMARY KEY CLUSTERED 
(
	[PartyAddressId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyRelationship]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyRelationship](
	[PartyRelationshipId] [int] IDENTITY(1,1) NOT NULL,
	[PartyRelationshipTypeId] [int] NOT NULL,
	[ClientPartyRoleId] [int] NOT NULL,
	[SupplierPartyRoleId] [int] NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyRelationship] PRIMARY KEY CLUSTERED 
(
	[PartyRelationshipId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyPreference]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyPreference](
	[PartyPreferenceId] [int] IDENTITY(1,1) NOT NULL,
	[PartyId] [int] NOT NULL,
	[PreferenceTypeId] [int] NOT NULL,
	[OptionTypeId] [int] NOT NULL,
	[Preference] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyPreference] PRIMARY KEY CLUSTERED 
(
	[PartyPreferenceId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PreferenceOption]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PreferenceOption](
	[PreferenceOptionId] [int] IDENTITY(1,1) NOT NULL,
	[PreferenceTypeId] [int] NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PreferenceOption] PRIMARY KEY CLUSTERED 
(
	[PreferenceOptionId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Note].[Note]') AND type in (N'U'))
BEGIN
CREATE TABLE [Note].[Note](
	[NoteId] [int] IDENTITY(1,1) NOT NULL,
	[Title] [nvarchar](100) NOT NULL,
	[Subject] [nvarchar](2000) NOT NULL,
	[NoteStatusId] [int] NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_Note_CreatedDateTime]  DEFAULT (getdate()),
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_Note_UpdatedDateTime]  DEFAULT (getdate()),
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_Note] PRIMARY KEY CLUSTERED 
(
	[NoteId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyRole]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyRole](
	[PartyRoleId] [int] IDENTITY(1,1) NOT NULL,
	[PartyId] [int] NOT NULL,
	[RoleTypeId] [int] NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyRole] PRIMARY KEY CLUSTERED 
(
	[PartyRoleId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Party].[PartyRole]') AND name = N'IX_Party_PartyRole')
CREATE UNIQUE NONCLUSTERED INDEX [IX_Party_PartyRole] ON [Party].[PartyRole] 
(
	[PartyId] ASC,
	[PartyRoleId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyTaxation]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyTaxation](
	[PartyId] [int] NOT NULL,
	[TaxFileNumber] [varchar](50) NOT NULL,
	[TaxationValidationTypeId] [int] NULL,
	[Exempt] [bit] NOT NULL,
	[EffectiveDateTime] [datetime] NOT NULL,
	[TaxationCountryId] [int] NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyTaxation] PRIMARY KEY CLUSTERED 
(
	[PartyId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyPartyClassificationRelation]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyPartyClassificationRelation](
	[PartyPartyClassificationRelationId] [int] IDENTITY(1,1) NOT NULL,
	[PartyId] [int] NOT NULL,
	[PartyClassificationId] [int] NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyPartyClassificationRelation] PRIMARY KEY CLUSTERED 
(
	[PartyPartyClassificationRelationId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Address].[Address]') AND type in (N'U'))
BEGIN
CREATE TABLE [Address].[Address](
	[AddressId] [int] IDENTITY(1,1) NOT NULL,
	[AddressTypeId] [int] NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_Address_1] PRIMARY KEY CLUSTERED 
(
	[AddressId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Address].[ElectronicAddress]') AND type in (N'U'))
BEGIN
CREATE TABLE [Address].[ElectronicAddress](
	[AddressId] [int] NOT NULL,
	[ElectronicAddressTypeId] [int] NOT NULL,
	[Detail] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_ElectronicAddress] PRIMARY KEY CLUSTERED 
(
	[AddressId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Address].[StateProvince]') AND type in (N'U'))
BEGIN
CREATE TABLE [Address].[StateProvince](
	[StateProvinceId] [int] IDENTITY(1,1) NOT NULL,
	[CountryId] [int] NOT NULL,
	[StateProvinceCode] [nvarchar](3) NOT NULL,
	[Description] [nvarchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_StateProvince] PRIMARY KEY CLUSTERED 
(
	[StateProvinceId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Address].[GeographicAddress]') AND type in (N'U'))
BEGIN
CREATE TABLE [Address].[GeographicAddress](
	[AddressId] [int] NOT NULL,
	[Line1] [varchar](60) NOT NULL,
	[Line2] [varchar](60) NOT NULL,
	[Line3] [varchar](60) NOT NULL,
	[Suburb] [varchar](60) NOT NULL,
	[State] [varchar](50) NOT NULL,
	[Postcode] [varchar](50) NOT NULL,
	[CountryId] [int] NOT NULL,
	[DPID] [varchar](50) NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_Address] PRIMARY KEY CLUSTERED 
(
	[AddressId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Address].[GeographicAddress]') AND name = N'IX_GeographicAddress')
CREATE NONCLUSTERED INDEX [IX_GeographicAddress] ON [Address].[GeographicAddress] 
(
	[AddressId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyCountryRelationship]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyCountryRelationship](
	[PartyCountryRelationshipId] [int] IDENTITY(1,1) NOT NULL,
	[PartyId] [int] NOT NULL,
	[CountryId] [int] NOT NULL,
	[PartyCountryRelationshipTypeId] [int] NOT NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_PartyCountryRelationship_CreatedDateTime]  DEFAULT (getdate()),
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_PartyCountryRelationship_UpdatedDateTime]  DEFAULT (getdate()),
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyCountryRelationship] PRIMARY KEY CLUSTERED 
(
	[PartyCountryRelationshipId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Party].[PartyCountryRelationship]') AND name = N'IX_PartyCountryRelationship')
CREATE UNIQUE NONCLUSTERED INDEX [IX_PartyCountryRelationship] ON [Party].[PartyCountryRelationship] 
(
	[PartyId] ASC,
	[CountryId] ASC,
	[PartyCountryRelationshipTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Party].[PartyCountryRelationship]') AND name = N'IX_PartyCountryRelationshipType')
CREATE NONCLUSTERED INDEX [IX_PartyCountryRelationshipType] ON [Party].[PartyCountryRelationship] 
(
	[PartyId] ASC,
	[PartyCountryRelationshipTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[PartyClassification]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[PartyClassification](
	[PartyClassificationId] [int] IDENTITY(1,1) NOT NULL,
	[PartyClassificationTypeId] [int] NOT NULL,
	[Code] [nvarchar](50) NOT NULL,
	[Description] [varchar](100) NOT NULL,
	[ParentPartyClassificationId] [int] NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_PartyClassification_CreatedDateTime]  DEFAULT (getdate()),
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL CONSTRAINT [DF_PartyClassification_UpdatedDateTime]  DEFAULT (getdate()),
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_PartyClassification] PRIMARY KEY NONCLUSTERED 
(
	[PartyClassificationId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Party].[PartyClassification]') AND name = N'IX_PartyClassification_Description')
CREATE CLUSTERED INDEX [IX_PartyClassification_Description] ON [Party].[PartyClassification] 
(
	[Description] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Party].[PartyClassification]') AND name = N'IX_PartyClassificationDescParentType')
CREATE UNIQUE NONCLUSTERED INDEX [IX_PartyClassificationDescParentType] ON [Party].[PartyClassification] 
(
	[Description] ASC,
	[PartyClassificationTypeId] ASC,
	[ParentPartyClassificationId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[OrganisationParty]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[OrganisationParty](
	[PartyId] [int] NOT NULL,
	[Name] [varchar](300) NOT NULL,
	[Number] [varchar](100) NULL,
	[EstablishmentDate] [datetime] NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_OrganisationParty] PRIMARY KEY CLUSTERED 
(
	[PartyId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Party].[Party]') AND type in (N'U'))
BEGIN
CREATE TABLE [Party].[Party](
	[PartyId] [int] IDENTITY(1,1) NOT NULL,
	[PartyTypeId] [int] NOT NULL,
	[PartyStatusId] [int] NULL,
	[CreatedBy] [varchar](50) NOT NULL,
	[CreatedDateTime] [datetime] NOT NULL,
	[UpdatedBy] [varchar](50) NOT NULL,
	[UpdatedDateTime] [datetime] NOT NULL,
	[RowTimestamp] [timestamp] NOT NULL,
 CONSTRAINT [PK_Party] PRIMARY KEY CLUSTERED 
(
	[PartyId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyNote_Note]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyNote]'))
ALTER TABLE [Party].[PartyNote]  WITH CHECK ADD  CONSTRAINT [FK_PartyNote_Note] FOREIGN KEY([NoteId])
REFERENCES [Note].[Note] ([NoteId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyNote_Party]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyNote]'))
ALTER TABLE [Party].[PartyNote]  WITH CHECK ADD  CONSTRAINT [FK_PartyNote_Party] FOREIGN KEY([PartyId])
REFERENCES [Party].[Party] ([PartyId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyIdentifier_IdentiferType]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyIdentifier]'))
ALTER TABLE [Party].[PartyIdentifier]  WITH CHECK ADD  CONSTRAINT [FK_PartyIdentifier_IdentiferType] FOREIGN KEY([IdentifierTypeId])
REFERENCES [Party].[IdentifierType] ([IdentifierTypeId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyIdentifier_Party]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyIdentifier]'))
ALTER TABLE [Party].[PartyIdentifier]  WITH CHECK ADD  CONSTRAINT [FK_PartyIdentifier_Party] FOREIGN KEY([PartyId])
REFERENCES [Party].[Party] ([PartyId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyIdentifier_RegistrationAuthority]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyIdentifier]'))
ALTER TABLE [Party].[PartyIdentifier]  WITH CHECK ADD  CONSTRAINT [FK_PartyIdentifier_RegistrationAuthority] FOREIGN KEY([RegistrationAuthorityId])
REFERENCES [Party].[RegistrationAuthority] ([RegistrationAuthorityId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_IndividualParty_GenderType]') AND parent_object_id = OBJECT_ID(N'[Party].[IndividualParty]'))
ALTER TABLE [Party].[IndividualParty]  WITH CHECK ADD  CONSTRAINT [FK_IndividualParty_GenderType] FOREIGN KEY([GenderId])
REFERENCES [Party].[Gender] ([GenderId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_IndividualParty_MartitalStatus]') AND parent_object_id = OBJECT_ID(N'[Party].[IndividualParty]'))
ALTER TABLE [Party].[IndividualParty]  WITH CHECK ADD  CONSTRAINT [FK_IndividualParty_MartitalStatus] FOREIGN KEY([MaritalStatusId])
REFERENCES [Party].[MaritalStatus] ([MaritialStatusId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_IndividualParty_Occupation]') AND parent_object_id = OBJECT_ID(N'[Party].[IndividualParty]'))
ALTER TABLE [Party].[IndividualParty]  WITH CHECK ADD  CONSTRAINT [FK_IndividualParty_Occupation] FOREIGN KEY([OccupationId])
REFERENCES [Party].[Occupation] ([OccupationId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_IndividualParty_Party]') AND parent_object_id = OBJECT_ID(N'[Party].[IndividualParty]'))
ALTER TABLE [Party].[IndividualParty]  WITH CHECK ADD  CONSTRAINT [FK_IndividualParty_Party] FOREIGN KEY([PartyId])
REFERENCES [Party].[Party] ([PartyId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyAddress_Address1]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyAddress]'))
ALTER TABLE [Party].[PartyAddress]  WITH NOCHECK ADD  CONSTRAINT [FK_PartyAddress_Address1] FOREIGN KEY([AddressId])
REFERENCES [Address].[Address] ([AddressId])
GO
ALTER TABLE [Party].[PartyAddress] CHECK CONSTRAINT [FK_PartyAddress_Address1]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyAddress_AddressUsage]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyAddress]'))
ALTER TABLE [Party].[PartyAddress]  WITH CHECK ADD  CONSTRAINT [FK_PartyAddress_AddressUsage] FOREIGN KEY([PartyAddressUsageId])
REFERENCES [Party].[PartyAddressUsage] ([PartyAddressUsageId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyAddress_Party]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyAddress]'))
ALTER TABLE [Party].[PartyAddress]  WITH NOCHECK ADD  CONSTRAINT [FK_PartyAddress_Party] FOREIGN KEY([PartyId])
REFERENCES [Party].[Party] ([PartyId])
GO
ALTER TABLE [Party].[PartyAddress] CHECK CONSTRAINT [FK_PartyAddress_Party]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyRelationship_PartyRelationshipType]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyRelationship]'))
ALTER TABLE [Party].[PartyRelationship]  WITH CHECK ADD  CONSTRAINT [FK_PartyRelationship_PartyRelationshipType] FOREIGN KEY([PartyRelationshipTypeId])
REFERENCES [Party].[PartyRelationshipType] ([PartyRelationshipTypeId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyRelationship_PartyRoleClient]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyRelationship]'))
ALTER TABLE [Party].[PartyRelationship]  WITH CHECK ADD  CONSTRAINT [FK_PartyRelationship_PartyRoleClient] FOREIGN KEY([ClientPartyRoleId])
REFERENCES [Party].[PartyRole] ([PartyRoleId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyRelationship_PartyRoleSupplier]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyRelationship]'))
ALTER TABLE [Party].[PartyRelationship]  WITH CHECK ADD  CONSTRAINT [FK_PartyRelationship_PartyRoleSupplier] FOREIGN KEY([SupplierPartyRoleId])
REFERENCES [Party].[PartyRole] ([PartyRoleId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyPreference_Party]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyPreference]'))
ALTER TABLE [Party].[PartyPreference]  WITH CHECK ADD  CONSTRAINT [FK_PartyPreference_Party] FOREIGN KEY([PartyId])
REFERENCES [Party].[Party] ([PartyId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyPreference_PreferenceType]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyPreference]'))
ALTER TABLE [Party].[PartyPreference]  WITH CHECK ADD  CONSTRAINT [FK_PartyPreference_PreferenceType] FOREIGN KEY([PreferenceTypeId])
REFERENCES [Party].[PreferenceType] ([PreferenceTypeId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PreferenceOption_PreferenceType]') AND parent_object_id = OBJECT_ID(N'[Party].[PreferenceOption]'))
ALTER TABLE [Party].[PreferenceOption]  WITH CHECK ADD  CONSTRAINT [FK_PreferenceOption_PreferenceType] FOREIGN KEY([PreferenceTypeId])
REFERENCES [Party].[PreferenceType] ([PreferenceTypeId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Note].[FK_Note_NoteStatus]') AND parent_object_id = OBJECT_ID(N'[Note].[Note]'))
ALTER TABLE [Note].[Note]  WITH CHECK ADD  CONSTRAINT [FK_Note_NoteStatus] FOREIGN KEY([NoteStatusId])
REFERENCES [Note].[NoteStatus] ([NoteStatusId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyRole_Party]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyRole]'))
ALTER TABLE [Party].[PartyRole]  WITH CHECK ADD  CONSTRAINT [FK_PartyRole_Party] FOREIGN KEY([PartyId])
REFERENCES [Party].[Party] ([PartyId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyRole_RoleType]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyRole]'))
ALTER TABLE [Party].[PartyRole]  WITH CHECK ADD  CONSTRAINT [FK_PartyRole_RoleType] FOREIGN KEY([RoleTypeId])
REFERENCES [Party].[RoleType] ([RoleTypeId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyTaxation_Party]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyTaxation]'))
ALTER TABLE [Party].[PartyTaxation]  WITH CHECK ADD  CONSTRAINT [FK_PartyTaxation_Party] FOREIGN KEY([PartyId])
REFERENCES [Party].[Party] ([PartyId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyTaxation_TFNValidationType]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyTaxation]'))
ALTER TABLE [Party].[PartyTaxation]  WITH CHECK ADD  CONSTRAINT [FK_PartyTaxation_TFNValidationType] FOREIGN KEY([TaxationValidationTypeId])
REFERENCES [Party].[TaxationValidationType] ([TaxationValidationTypeId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyPartyClassificationRelation_Party]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyPartyClassificationRelation]'))
ALTER TABLE [Party].[PartyPartyClassificationRelation]  WITH CHECK ADD  CONSTRAINT [FK_PartyPartyClassificationRelation_Party] FOREIGN KEY([PartyId])
REFERENCES [Party].[Party] ([PartyId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyPartyClassificationRelation_PartyClassification]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyPartyClassificationRelation]'))
ALTER TABLE [Party].[PartyPartyClassificationRelation]  WITH CHECK ADD  CONSTRAINT [FK_PartyPartyClassificationRelation_PartyClassification] FOREIGN KEY([PartyClassificationId])
REFERENCES [Party].[PartyClassification] ([PartyClassificationId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Address].[FK_Address_AddressType]') AND parent_object_id = OBJECT_ID(N'[Address].[Address]'))
ALTER TABLE [Address].[Address]  WITH NOCHECK ADD  CONSTRAINT [FK_Address_AddressType] FOREIGN KEY([AddressTypeId])
REFERENCES [Address].[AddressType] ([AddressTypeId])
GO
ALTER TABLE [Address].[Address] CHECK CONSTRAINT [FK_Address_AddressType]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Address].[FK_ElectronicAddress_Address]') AND parent_object_id = OBJECT_ID(N'[Address].[ElectronicAddress]'))
ALTER TABLE [Address].[ElectronicAddress]  WITH NOCHECK ADD  CONSTRAINT [FK_ElectronicAddress_Address] FOREIGN KEY([AddressId])
REFERENCES [Address].[Address] ([AddressId])
GO
ALTER TABLE [Address].[ElectronicAddress] CHECK CONSTRAINT [FK_ElectronicAddress_Address]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Address].[FK_ElectronicAddress_ElectronicAddressType]') AND parent_object_id = OBJECT_ID(N'[Address].[ElectronicAddress]'))
ALTER TABLE [Address].[ElectronicAddress]  WITH CHECK ADD  CONSTRAINT [FK_ElectronicAddress_ElectronicAddressType] FOREIGN KEY([ElectronicAddressTypeId])
REFERENCES [Address].[ElectronicAddressType] ([ElectronicAddressTypeId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Address].[FK_StateProvince_Country]') AND parent_object_id = OBJECT_ID(N'[Address].[StateProvince]'))
ALTER TABLE [Address].[StateProvince]  WITH CHECK ADD  CONSTRAINT [FK_StateProvince_Country] FOREIGN KEY([CountryId])
REFERENCES [Address].[Country] ([CountryId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Address].[FK_GeographicAddress_Address]') AND parent_object_id = OBJECT_ID(N'[Address].[GeographicAddress]'))
ALTER TABLE [Address].[GeographicAddress]  WITH NOCHECK ADD  CONSTRAINT [FK_GeographicAddress_Address] FOREIGN KEY([AddressId])
REFERENCES [Address].[Address] ([AddressId])
GO
ALTER TABLE [Address].[GeographicAddress] CHECK CONSTRAINT [FK_GeographicAddress_Address]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Address].[FK_GeographicAddress_Country]') AND parent_object_id = OBJECT_ID(N'[Address].[GeographicAddress]'))
ALTER TABLE [Address].[GeographicAddress]  WITH NOCHECK ADD  CONSTRAINT [FK_GeographicAddress_Country] FOREIGN KEY([CountryId])
REFERENCES [Address].[Country] ([CountryId])
GO
ALTER TABLE [Address].[GeographicAddress] CHECK CONSTRAINT [FK_GeographicAddress_Country]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyCountryRelationship_Country]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyCountryRelationship]'))
ALTER TABLE [Party].[PartyCountryRelationship]  WITH CHECK ADD  CONSTRAINT [FK_PartyCountryRelationship_Country] FOREIGN KEY([CountryId])
REFERENCES [Address].[Country] ([CountryId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyCountryRelationship_Party]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyCountryRelationship]'))
ALTER TABLE [Party].[PartyCountryRelationship]  WITH CHECK ADD  CONSTRAINT [FK_PartyCountryRelationship_Party] FOREIGN KEY([PartyId])
REFERENCES [Party].[Party] ([PartyId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyCountryRelationship_RegistrationClassification]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyCountryRelationship]'))
ALTER TABLE [Party].[PartyCountryRelationship]  WITH CHECK ADD  CONSTRAINT [FK_PartyCountryRelationship_RegistrationClassification] FOREIGN KEY([PartyCountryRelationshipTypeId])
REFERENCES [Party].[PartyCountryRelationshipType] ([PartyCountryRelationshipTypeId])
GO

GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyClassification_PartyClassification]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyClassification]'))
ALTER TABLE [Party].[PartyClassification]  WITH CHECK ADD  CONSTRAINT [FK_PartyClassification_PartyClassification] FOREIGN KEY([ParentPartyClassificationId])
REFERENCES [Party].[PartyClassification] ([PartyClassificationId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_PartyClassification_PartyClassificationType]') AND parent_object_id = OBJECT_ID(N'[Party].[PartyClassification]'))
ALTER TABLE [Party].[PartyClassification]  WITH CHECK ADD  CONSTRAINT [FK_PartyClassification_PartyClassificationType] FOREIGN KEY([PartyClassificationTypeId])
REFERENCES [Party].[PartyClassificationType] ([PartyClassificationTypeId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_OrganisationParty_Party]') AND parent_object_id = OBJECT_ID(N'[Party].[OrganisationParty]'))
ALTER TABLE [Party].[OrganisationParty]  WITH CHECK ADD  CONSTRAINT [FK_OrganisationParty_Party] FOREIGN KEY([PartyId])
REFERENCES [Party].[Party] ([PartyId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[Party].[FK_Party_PartyType]') AND parent_object_id = OBJECT_ID(N'[Party].[Party]'))
ALTER TABLE [Party].[Party]  WITH CHECK ADD  CONSTRAINT [FK_Party_PartyType] FOREIGN KEY([PartyTypeId])
REFERENCES [Party].[PartyType] ([PartyTypeId])

go